<%@ page contentType="text/html; charset=UTF-8"%>
<html>
	<head>
		<OBJECT ID='GDCACom'
			CLASSID='clsid:6DD195B3-1B23-468A-9049-C29CFCDB4FE2' width='0' height='0'></OBJECT>
		<title>测试天翼税通接口</title>
	</head>
	<script src="/tyst-demo/gdca/js/PkiOper.js"></script>
	<script src="/tyst-demo/gdca/js/util.js"></script>

	<body>
		<br>
		<form id="flowForm" method="post">		
			<input type="hidden" id="busyType" name="busyType" value="">
			<input type="hidden" id="initPassword" name="initPassword" value="">
			<input type="hidden" id="userPassword" name="userPassword" value="">
			<input type="hidden" id="trustId" name="trustId" value="">	
			<input type="hidden" id="publicKey" name="publicKey" value="">			
			<input type="hidden" id="cn" name="cn" value="">
			<input type="hidden" id="o" name="o" value="">
			<input type="hidden" id="ou" name="ou" value="">
			<input type="hidden" id="c" name="c" value="">
			<input type="hidden" id="s" name="s" value="">
			<input type="hidden" id="l" name="l" value="">
			<input type="hidden" id="e" name="e" value="">
			<input type="hidden" id="nsrdjh" name="nsrdjh" value="">
			<input type="hidden" id="zsh" name="zsh" value="">			
			<table>
				<tr>
					<td align="middle" cols="2">
						<b><font color="red">注意：新增后，GDCA将返回证书相关信息，把证书相关信息写到KEY里后，<br>您的系统还必须保存信任服务号，因为其他业务必须发送信任服务号给GDCA。</font>
						</b>
					</td>
				</tr>

				<tr>
					<td align="right">
						信任服务号(除新增外，**必填**)：
					</td>
					<td>
						<input type="text" id="trustIdForm" name="trustIdForm" size="60"><br>
						<input type="button" value="从KEY获取信任服务号"
							onclick="return getTrustId();">
						<input type="button" value="从您的数据库里信任服务号"
							onclick="return getTrustId();">
					</td>
				</tr>
				<tr>
					<td align="right">
						CN项(证书通用名，**必填**)：
					</td>
					<td align="left">
						<input type="text" id="cnForm" name="cnForm" size="60" value="CN项">
					</td>
				</tr>
				<tr>
					<td align="right">
						O项：
					</td>
					<td align="left">
						<input type="text" id="oForm" name="oForm" size="60"  value="O项">
					</td>
				</tr>
				<tr>
					<td align="right">
						OU项：
					</td>
					<td align="left">
						<input type="text" id="ouForm" name="ouForm" size="60"  value="OU项">
					</td>
				</tr>
				<tr>
					<td align="right">
						C项（国家）：
					</td>
					<td align="left">
						<input type="text" id="cForm" name="cForm" value="CN" readOnly size="60">
					</td>
				</tr>
				<tr>
					<td align="right">
						S项（省份）：
					</td>
					<td align="left">
						<input type="text" id="sForm" name="sForm" size="60"  value="广东省">
					</td>
				</tr>
				<tr>
					<td align="right">
						L项（城市）：
					</td>
					<td align="left">
						<input type="text" id="lForm" name="lForm" size="60"  value="广州市">
					</td>
				</tr>
				<tr>
					<td align="right">
						E项（邮箱）：
					</td>
					<td align="left">
						<input type="text" id="eForm" name="eForm" size="60"  value="gdca@gdca.com.cn">
					</td>
				</tr>
				<tr>
					<td align="right">
						纳税人登记号：
					</td>
					<td align="left">
						<input type="text" id="nsrdjhForm" name="nsrdjhForm" size="60"  value="nasrjh-001">
					</td>
				</tr>
				<tr>
					<td align="right">
						子税号：
					</td>
					<td align="left">
						<input type="text" id="zshForm" name="zshForm" size="60"  value="zsh-001">
					</td>
				</tr>
				<tr>
					<td align="middle" cols="2">
						<input type="button" value="新增" onclick="return certBusy('001');">
						<input type="button" value="资料更新" onclick="return certBusy('002');">
						<input type="button" value="注销" onclick="return certBusy('003');">
						<input type="button" value="到期更新" onclick="return certBusy('004');">
						<input type="button" value="补发" onclick="return certBusy('005');">						
						<input type="button" value="解锁" onclick="return certBusy('006');">
					</td>
				</tr>
			</table>
		</form>
		<iframe name="theFrame" id="theFrame" height="0" width="0"
			frameborder="0"></iframe>
	</body>
	<script>
	function certBusy(busyTypeCode)
	{
		var busyType = busyTypeCode;
		try{		
		    document.getElementById("trustId").value=document.getElementById("trustIdForm").value;
		    if(busyType=="001")
			{
				 document.getElementById("trustId").value="";
			}
			if(busyType!="001" && document.getElementById("trustId").value=="")
			{
			     //alert("请获取信任服务号");
		         hideDiv("请获取信任服务号", "");
		         return 1;
			}
			
		    showdiv("处理中，请稍候......");  
		    if(busyType!="003")//非注销业务
		    {
				GDCAFinalize(GDCACom);
				var out_data = new Array;
				var initPassword = "";
				var userPassword = "123456";
			    var key_type = isSGKey(GDCACom);
			    if(!key_type)
			    {
			    	hideDiv("key_type=",key_type);
			    	return 1;
			    }
			    ret=GDCASetDevice(GDCACom,key_type);
			    if(ret!=0)
			    {
			       //alert("设置设备类型出错："+ret);	
			       hideDiv("设置设备类型出错：",ret);	       
			       return ret;
			    }	
			    ret=GDCAInitialize(GDCACom);
			    if(ret!=0)
			    {
			        //alert("初始化控件出错："+ret);		
			        hideDiv("初始化控件出错：",ret);	         
			        return ret;
			    }
				if(busyType=="001" || busyType=="005")//新增或补发，要格式化key
				{
				 	   var out_data2 = new Array;
					   GDCAReadOperatorLabel(GDCACom,out_data);
					   if(out_data[0])
					   {
					      GDCABase64Decode(GDCACom,out_data[0],out_data2);
					      if(out_data2[0] == 1)
					      {
					         //alert("不能格式化操作员key");
					         hideDiv("不能格式化操作员key",0);
					         return 1;
					      }
					   }
					   //var encCert="MIIDizCCAvSgAwIBAgIPByAAIAcFGBAAEAAAA4RIMA0GCSqGSIb3DQEBBQUAMIIBJDENMAsGA1UEBh4EAEMATjEbMBkGA1UECB4SAEcAdQBhAG4AZwBkAG8AbgBnMRswGQYDVQQHHhIARwB1AGEAbgBnAHoAaABvAHUxPTA7BgNVBAoeNABHAEQAQwBBACAAQwBlAHIAdABpAGYAaQBjAGEAdABlACAAQQB1AHQAaABvAHIAaQB0AHkxRzBFBgNVBAsePgBHAHUAYQBuAGcAZABvAG4AZwAgAEMAZQByAHQAaQBmAGkAYwBhAHQAZQAgAEEAdQB0AGgAbwByAGkAdAB5MVEwTwYDVQQDHkgARwBEAEMAQQAgAEcAdQBhAG4AZwBkAG8AbgBnACAAQwBlAHIAdABpAGYAaQBjAGEAdABlACAAQQB1AHQAaABvAHIAaQB0AHkwHhcNMDcwNTE4MTA1MDEwWhcNMDkwNTE4MTA1MDEwWjAwMREwDwYDVQQLHggARwBEAEMAQTEbMBkGA1UE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ZwBkAG8AbgBnACAAQwBlAHIAdABpAGYAaQBjAGEAdABlACAAQQB1AHQAaABvAHIAaQB0AHkwHhcNMDkwMzE3MTYwMDAwWhcNMTEwMzE3MTYwMDAwWjBvMQ0wCwYDVQQGHgQAQwBOMQ8wDQYDVQQIHgZef04cdwExDzANBgNVBAceBl5/Xd5eAjEdMBsGA1UECh4UAEcARABDAEF/UU4KThpSoYvBTmYxHTAbBgNVBAMeFABHAEQAQwBBf1FOCk4aUqGLwU5mMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCpCfHOawQrzLSXdSfcpj0IzgqaFC6lkJIKJQe7KeSR8X9sQmAPnJmP33Tq5QqLRbxQzXk0xIm5tDBK+rrJ2NpBh2vYEJBV7sHLG0DP0x9BOgeNognHdiukSRm73y0FlsZjjLi8ESnHEL9Xq7WWABefSS5C7yCbi4JjNzqwdMix6QIDAQABo4GbMIGYMB8GA1UdIwQYMBaAFHdDCRD3FYs6sBHSTHsHlATwFFRGMB0GA1UdDgQWBBRqGTvCL4cSYE7/x89wW/s37BUT1jALBgNVHQ8EBAMCBDAwFQYFKlYLBwMEDBMKR0RDQVEKGqHBZjAVBgUqVhUBAgQMZ2RzYXQuY29tLmNuMBsGBSpWFQEDBBIwMDcyMDIwMDkwMzE4MDA5NTcwDQYJKoZIhvcNAQEFBQADgYEAAUgiove8p3e4EBeSbZmGdTmXFpNICjQN3hfzUZFuVRXbecKqueepFGjzkpTIzOHJKjt2NUuiW9xzEBJBh810uIwou0ZdI7yS2aSXMp1CehGDS72tG7aC0Hke9HPtl1TVvMBMwEChhSJXh8ax77FMM7H1VOWefu/sS8qyntHI2sE=";
					   var superPinEnv = new Array;
					   superPinEnv[0] = "";	
					   superPinEnv[0] = GDCACom.GDCA_FormatDeviceRetSuperPin(encCert, userPassword);
					   ret = GDCACom.GetError();
					   if(ret!=0)
					   {
					        alert("superPin数字信封获取失败，ret = " + ret );
						    hideDiv("superPin数字信封获取失败，ret =：",ret);
						    return ret;
					   }
					   //alert("superPin数字信封获取成功！" + superPinEnv[0]);
					   initPassword = superPinEnv[0];
					   
					   //登录
					    ret = GDCALogin(GDCACom,2,userPassword);
					    if(ret!=0)
					    {
					        //alert("登陆出错："+ret);
					        hideDiv("登陆出错：",ret);
					        GDCAFinalize(GDCACom);
					        return ret;
					    }
					    
					   //产生密钥对
					   ret = GDCAFormatRAEKey(GDCACom);
					   if(ret!=0)
					   {
					        //alert("生成签名密钥对、临时加密密钥对出错："+ret);
					        hideDiv("生成签名密钥对、临时加密密钥对出错：",ret);
					        GDCALogout(GDCACom);
					        GDCAFinalize(GDCACom);
					        return ret;
					   }
				 }
				 
				 //获取签名公钥
				 var unsymmkey = new Array;
				 ret = GDCAGetPubKey(GDCACom,LBL_USERCERT_SIG,GDCA_LBL_SIGNKEY,unsymmkey);
			     if(ret!=0)
			     {
			            //alert("获得签名证书公钥出错："+ret);
			            hideDiv("获得签名证书公钥出错：",ret);
			            GDCACommonEndKey(GDCACom);
			            return ret;
			     }
			     //alert(unsymmkey[0]);
				 document.getElementById("publicKey").value=unsymmkey[0];
			 }
			 else
			 {
			 	document.getElementById("publicKey").value="";
			 }
			 if(busyType=="001" || busyType=="005")
			 {
			 	document.getElementById("initPassword").value=initPassword;
			 }
			 else
			 {
			 	document.getElementById("initPassword").value="";
			 }
			 document.getElementById("busyType").value=busyType;
			 document.getElementById("cn").value=document.getElementById("cnForm").value;
			 document.getElementById("o").value=document.getElementById("oForm").value;
			 document.getElementById("ou").value=document.getElementById("ouForm").value;
			 document.getElementById("c").value=document.getElementById("cForm").value;
			 document.getElementById("s").value=document.getElementById("sForm").value;
			 document.getElementById("l").value=document.getElementById("lForm").value;
			 document.getElementById("e").value=document.getElementById("eForm").value;
			 document.getElementById("nsrdjh").value=document.getElementById("nsrdjhForm").value;
			 document.getElementById("zsh").value=document.getElementById("zshForm").value;		
			 	 
			 document.getElementById("flowForm").target = "theFrame";			 		     
		     document.getElementById("flowForm").action = "/tyst-demo/gdca/GdcaServlet";		     
		     document.getElementById("flowForm").submit();
		     document.getElementById("flowForm").target = "";
		     document.getElementById("flowForm").action = "";
		 }
	     catch(e)
	     {
	     	hideDiv("异常：",e.message);
	     } 
	     finally 
	     {
		    GDCAFinalize(GDCACom);
	     }
	}	
	
	function getTrustId()
	{
		try{
			GDCAFinalize(GDCACom);
			var out_data = new Array;
		    var key_type = isSGKey(GDCACom);
		    if(!key_type)
		    {
		    	hideDiv("key_type=",key_type);
		    	return 1;
		    }
		    ret=GDCASetDevice(GDCACom,key_type);
		    if(ret!=0)
		    {
		       alert("设置设备类型出错："+ret);	       
		       return ret;
		    }	
		    ret=GDCAInitialize(GDCACom);
		    if(ret!=0)
		    {
		        alert("初始化控件出错："+ret);	         
		        return ret;
		    }
		    var cert = GDCACom.GDCA_ReadLabel("LAB_USERCERT_SIG",7);
		    ret=GDCACom.GetError();
		    if(ret!=0)
		    {
		    	alert("获取签名证书失败");
		    	return ret;
		    }
		    var trustid1 =GDCACom.GDCA_GetInfoByOID(cert,2,"1.2.86.21.1.1",0);
	        var trustid3=GDCACom.GDCA_GetInfoByOID(cert,2,"1.2.86.21.1.3",0);
	        trustid = trustid3 + trustid1;
	        if(trustid.length==20)
	        {
	        	trustid = trustid.substring(2,20);
	        }
	        document.getElementById("trustIdForm").value=trustid;
	        
	    }
	    catch(e)
	    {
	    
	    }
	    finally 
        {
	        GDCAFinalize(GDCACom);
        }
	}
	
	function hideDiv(retValue, ret)
	{
		alert(retValue + "" + ret);
		closediv();	
	}
	
	</script>
</html>
